//
//  WYJTabBarController.swift
//  MySinaWeibo
//
//  Created by believewilliam on 16/9/24.
//  Copyright © 2016年 believewilliam. All rights reserved.
//

import UIKit

class WYJTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        let mainTabBar = WYJMainTabBar()
        mainTabBar.composeClosure = {[weak self] in
//            print("撰写按钮被点击了",self)
//            print("~~~~~~~~~~~~~~~~~~~~~~")
            let composeView = WYJComposeView(frame: UIScreen.main.bounds)
            /* 添加到window中
            let window = UIApplication.shared.keyWindow
            window?.addSubview(composeView)
 */
            // 添加到self.view中
            composeView.locatedVC = self
            self?.view.addSubview(composeView)
        }
        setValue(mainTabBar, forKey: "tabBar")
        addChildViewControllers()
        //        tabBar.barTintColor = UIColor.orange
       
    }
    
    private func addChildViewControllers() {
        //        let homeVC = WYJHomeTableViewController()
        //        homeVC.title = "主页"
        //        homeVC.tabBarItem.title = "主页"
        //        homeVC.tabBarItem.image = #imageLiteral(resourceName: "tabbar_home")
        //        let  nav = UINavigationController(rootViewController: homeVC)
        //        addChildViewController(nav)
        addChildViewController(vc: WYJHomeTableViewController(), title: "主页", imageName: "tabbar_home", index: 0)
        addChildViewController(vc: WYJMessageTableViewController(), title: "消息", imageName: "tabbar_message_center", index: 1)
        addChildViewController(vc: WYJDiscoveryTableViewController(), title: "发现", imageName: "tabbar_discover", index: 2)
        addChildViewController(vc: WYJProfileTableViewController(), title: "我", imageName: "tabbar_profile", index: 3)
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String, index: Int) {
        vc.title = title
//        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.tag = index
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 11)], for: .normal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        //        vc.tabBarItem.badgeColor = UIColor.purple
        //        vc.tabBarItem.badgeValue = "2"
        let nav = WYJBaseNavgationController(rootViewController: vc)
        addChildViewController(nav)
    }
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        var index = 0
        for subview in tabBar.subviews {
            // 先找到UITabBarButton
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                // 再找到所点击的那个UITabBarButton
                if index == item.tag {
                    for target in subview.subviews {
//                        print(target)
                        // 再找到所点击的那个UITabBarButton的UITabBarSwappableImageView
                        if target.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            // 设置动画
                            target.transform = CGAffineTransform(scaleX: 0.4, y: 0.4)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: [], animations: {
                                target.transform = CGAffineTransform(scaleX: 1, y: 1)
                                }, completion: { (_) in
                                    
                            })
                        }
                    }
                }
                index += 1
            }
            
        }
    }
    
    deinit {
//        print("标签控制器")
    }
}
